package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.annotation.AnonymityAccess;
import cn.edu.sgu.www.pms.annotation.FeignService;
import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.UserDTO;
import cn.edu.sgu.www.pms.dto.UserRegisterDTO;
import cn.edu.sgu.www.pms.entity.User;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "用户管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/user", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("退出登录")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public JsonResult<Void> logout() {
        userService.logout();

        return JsonResult.success("退出成功");
    }

    @AnonymityAccess
    @ApiOperation("登录认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public JsonResult<Void> login(
            @NotBlank(message = "用户名" + CustomMessages.NOT_NULL) String username,
            @NotBlank(message = "密码" + CustomMessages.NOT_NULL) String password) {
        userService.login(username, password);

        return JsonResult.success("登录成功");
    }

    @ApiOperation("添加用户")
    @RequestMapping(path = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated UserDTO userDTO) {
        userService.insert(userDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID批量删除用户")
    @RequestMapping(path = "/deleteByIds", method = RequestMethod.POST)
    public JsonResult<Void> deleteByIds(
            @ApiParam(name = "ids", value = "用户ID列表", required = true)
            @Size(min = 1, message = "用户ID列表" + CustomMessages.NOT_NULL)
            @RequestParam("ids") List<String> ids) {
        userService.deleteByIds(ids);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改用户信息")
    @RequestMapping(path = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated UserDTO userDTO) {
        userService.updateById(userDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID查询用户信息")
    @RequestMapping(path = "/selectById", method = RequestMethod.GET)
    public JsonResult<User> selectById(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
        User user = userService.selectById(id);

        return JsonResult.success(null, user);
    }

    @ApiOperation("查询用户列表")
    @RequestMapping(path = "/selectList", method = RequestMethod.GET)
    public JsonResult<List<User>> selectList() {
        List<User> list = userService.selectList();

        return JsonResult.success(null, list);
    }

    @FeignService
    @ApiOperation("锁定用户")
    @RequestMapping(path = "/lock", method = RequestMethod.POST)
    public JsonResult<Void> lockUser(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @NotBlank(message = "用户" + CustomMessages.ID_NOT_NULL) String userId) {
        userService.lock(userId);

        return JsonResult.success();
    }

    @FeignService
    @ApiOperation("用户注册")
    @RequestMapping(path = "/register", method = RequestMethod.POST)
    public JsonResult<Void> register(@RequestBody @Validated UserRegisterDTO registerDTO) {
        userService.register(registerDTO);

        return JsonResult.success();
    }

    @FeignService
    @ApiOperation("修改用户最后一次登录时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true),
            @ApiImplicitParam(name = "lastLoginTime", value = "最后一次登录时间", required = true)
    })
    @RequestMapping(value = "/updateLastLoginTime", method = RequestMethod.POST)
    public JsonResult<Void> updateLastLoginTime(
            @NotBlank(message = "用户" + CustomMessages.ID_NOT_NULL) String userId,
            @NotNull(message = "登录时间" + CustomMessages.NOT_NULL) LocalDateTime lastLoginTime) {
        userService.updateLastLoginTime(userId, lastLoginTime);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @FeignService
    @ApiOperation("通过用户名修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "service", value = "应用名", required = true),
            @ApiImplicitParam(name = "user", value = "用户信息", required = true)
    })
    @RequestMapping(value = "/updateByUsername", method = RequestMethod.POST)
    public JsonResult<Void> updateByUsername(
            @NotNull(message = "用户信息" + CustomMessages.NOT_NULL) @RequestBody User user,
            @NotBlank(message = "应用名" + CustomMessages.NOT_NULL) String service) {
        userService.updateByUsername(user, service);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @FeignService
    @ApiOperation("根据用户名查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "service", value = "应用名", required = true)
    })
    @RequestMapping(value = "/selectByUsername", method = RequestMethod.GET)
    public JsonResult<User> selectByUsername(
            @NotBlank(message = "用户名" + CustomMessages.NOT_NULL) String username,
            @NotBlank(message = "应用名" + CustomMessages.NOT_NULL) String service) {
        User user = userService.selectByUsername(username, service);

        return JsonResult.success(null, user);
    }

}